package com.teacherprofile.manage.controller;

import java.util.List;

import com.teacherprofile.common.core.domain.model.LoginUser;
import com.teacherprofile.common.utils.SecurityUtils;
import jakarta.servlet.http.HttpServletResponse;

import com.teacherprofile.common.utils.StringUtils;

import com.teacherprofile.manage.domain.dto.CopyrightExportDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.teacherprofile.common.annotation.Log;
import com.teacherprofile.common.core.controller.BaseController;
import com.teacherprofile.common.core.domain.AjaxResult;
import com.teacherprofile.common.enums.BusinessType;
import com.teacherprofile.common.domain.SoftwareCopyrights;
import com.teacherprofile.manage.service.ISoftwareCopyrightsService;
import com.teacherprofile.common.utils.poi.ExcelUtil;
import com.teacherprofile.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 软著管理Controller
 *
 * @author qvtu
 * @date 2025-04-21
 */
@Api(tags = "软著管理")
@RestController
@RequestMapping("/manage/copyright")
public class SoftwareCopyrightsController extends BaseController {
    @Autowired
    private ISoftwareCopyrightsService softwareCopyrightsService;

    /**
     * 查询软著管理列表
     */
    @ApiOperation(value = "查询软著管理列表")
    @PreAuthorize("@ss.hasPermi('manage:copyright:list')")
    @GetMapping("/list")
    public TableDataInfo list(SoftwareCopyrights softwareCopyrights) {
        startPage();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        System.out.println(loginUser);
        List<SoftwareCopyrights> list = softwareCopyrightsService.selectSoftwareCopyrightsList(softwareCopyrights);
        return getDataTable(list);
    }

    /**
     * 导出软著管理列表
     */
    @ApiOperation(value = "导出软著管理列表")
    @PreAuthorize("@ss.hasPermi('manage:copyright:export')")
    @Log(title = "软著管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,  CopyrightExportDTO dto) {
        List<SoftwareCopyrights> list = softwareCopyrightsService.selectSoftwareCopyrightsList(dto);
        ExcelUtil<SoftwareCopyrights> util = new ExcelUtil<SoftwareCopyrights>(SoftwareCopyrights.class);
        util.exportEasyExcel(response, list, "软著管理数据", StringUtils.str2List(dto.getExportFields(), ",", true, true));
    }

    /**
     * 获取软著管理详细信息
     */
    @ApiOperation(value = "获取软著详细信息")
    @PreAuthorize("@ss.hasPermi('manage:copyright:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(softwareCopyrightsService.selectSoftwareCopyrightsById(id));
    }

    /**
     * 审批
     */
    @ApiOperation(value = "软著审批")
    @PreAuthorize("@ss.hasPermi('manage:copyright:check')")
    @Log(title = "软著管理", businessType = BusinessType.UPDATE)
    @PutMapping("/check")
    public AjaxResult check(@RequestBody SoftwareCopyrights softwareCopyrights) {
        return toAjax(softwareCopyrightsService.check(softwareCopyrights));
    }

    /**
     * 新增软著管理
     */
    @ApiOperation(value = "新增软著")
    @PreAuthorize("@ss.hasPermi('manage:copyright:add')")
    @Log(title = "软著管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SoftwareCopyrights softwareCopyrights) {
        return toAjax(softwareCopyrightsService.insertSoftwareCopyrights(softwareCopyrights));
    }

    /**
     * 修改软著管理
     */
    @ApiOperation(value = "修改软著")
    @PreAuthorize("@ss.hasPermi('manage:copyright:edit')")
    @Log(title = "软著管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SoftwareCopyrights softwareCopyrights) {
        return toAjax(softwareCopyrightsService.updateSoftwareCopyrights(softwareCopyrights));
    }

    /**
     * 删除软著管理
     */
    @ApiOperation("删除软著")
    @PreAuthorize("@ss.hasPermi('manage:copyright:remove')")
    @Log(title = "软著管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(softwareCopyrightsService.deleteSoftwareCopyrightsByIds(ids));
    }


    @ApiOperation("导入软著")
    @Log(title = "导入管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:user:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<SoftwareCopyrights> util = new ExcelUtil<SoftwareCopyrights>(SoftwareCopyrights.class);
        List<SoftwareCopyrights> softwareCopyrights = util.importEasyExcel(file.getInputStream());
        String message = softwareCopyrightsService.importData(softwareCopyrights);
        return success(message);
    }

}
